<template>
  <div class="h-full px-4 py-3 rounded-lg">
    <div class="flex justify-end">
      <div class="flex">
        <a-input
          v-model:value.trim="searchForm.reportName"
          placeholder="请输入报告名称"
          v-filter-special-chars
          class="_input"
        />
        <SerchButtom @search="getList(true, true)" @reset="reset" />
      </div>
    </div>

    <div class="mt-3 h-[calc(100%-6rem)] overflow-y-auto">
      <a-table
        :dataSource="dataSource"
        :pagination="false"
        :columns="columns"
        :loading="loading"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'order'">
            {{ (pagination.current - 1) * pagination.pageSize + index + 1 }}
          </template>
          <template v-else-if="column.dataIndex === 'operation'">
            <a-button type="link" @click="openModal('download', record)">
              <!-- 查看报告 -->
              下载报告
            </a-button>
            <a-button
              type="link"
              class="delete"
              @click="openModal('delete', record)"
            >
              删除
            </a-button>
          </template>
        </template>
      </a-table>
    </div>

    <div class="mt-3 h-8">
      <Pagination @getListByPage="getListByPage" ref="paginationRef" />
    </div>
  </div>

  <IndexModal ref="indexModalRef" @isOk="getList(true, true)" />
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'

import IndexModal from './IndexModal.vue'
import SerchButtom from '@/components/YuanQi-SearchButtom.vue'

import { fileUrl2Blob, download } from '@/utils/download'
import { list } from './serve.api'

const dataSource = ref()
const loading = ref<boolean>(false)
const paginationRef = ref()

const indexModalRef = ref()

let searchFormClone = {
  pageNum: 1,
  pageSize: 10,
  reportName: ''
}

// 搜索参数
const searchForm = reactive({
  reportName: ''
})

const columns = ref([
  {
    title: '序号',
    dataIndex: 'order',
    width: '7.5rem'
  },
  {
    title: '报告名称',
    dataIndex: 'reportName',
    key: 'reportName',
    ellipsis: true
  },
  {
    title: '生成时间',
    dataIndex: 'reportTime',
    key: 'reportTime',
    ellipsis: true
  },
  {
    title: '操作',
    dataIndex: 'operation',
    key: 'operation',
    ellipsis: true,
    width: '12rem'
  }
])

// 重置
const reset = () => {
  searchForm.reportName = ''
  getList(true, true)
}

const pagination = reactive({
  total: 0,
  pageSize: 10, // 每页中显示10条数据
  position: ['bottomCenter'],
  current: 1,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total: number) => `共 ${total} 条`
})

// 初始化数据
const getList = (flag = false, isSearch = false) => {
  if (flag) {
    pagination.current = 1
    paginationRef.value.changeCurrent(pagination.current)
  }
  loading.value = true
  dataSource.value = []

  const params = {
    reportName: searchForm.reportName,
    pageNum: pagination.current,
    pageSize: pagination.pageSize
  }
  if (isSearch) {
    searchFormClone = JSON.parse(JSON.stringify(params))
  } else {
    params.reportName = searchFormClone.reportName
  }

  list(params)
    .then((res) => {
      if (res) {
        console.log(res, '多终端融合巡视报告列表')

        dataSource.value = res.list
        pagination.total = res.total

        paginationRef.value.changeTotal(pagination.total)
        loading.value = false
      } else {
        loading.value = false
      }
    })
    .catch(() => {
      loading.value = false
    })
}

const getListByPage = (page: number, pageSize: number) => {
  if (pagination.current !== page) {
    pagination.current = page
    getList()
  }
  if (pagination.pageSize !== pageSize) {
    pagination.pageSize = pageSize
    getList(true)
  }
}

// 删除/查看操作
type ModalType = 'delete' | 'view' | 'download'

const openModal = async (type: ModalType, data?: any) => {
  if (type === 'download') {
    // TODO 下载报告
    const blob = await fileUrl2Blob(data.reportPath)
    const fileName = data.reportPath?.split('/').pop()
    download(decodeURIComponent(fileName), blob)
  } else {
    indexModalRef.value.showModal(type, data)
  }
}

onMounted(() => {
  getList(true, true)
})
</script>
<style lang="less" scoped>
._input {
  width: 13.125rem;
  vertical-align: middle;
}
</style>
